package com.probox.common.entity.service.order.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.probox.common.core.enums.PayMethodEnum;
import com.probox.common.core.enums.PayStatusEnum;
import com.probox.common.core.utils.DateUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.entity.domain.order.OrderInfoParent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.probox.common.entity.mapper.order.OrderPayInfoMapper;
import com.probox.common.entity.domain.order.OrderPayInfo;
import com.probox.common.entity.service.order.IOrderPayInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 付款流水Service业务层处理
 *
 * @author hds
 * @date 2021-06-07
 */
@Service
@Transactional
public class OrderPayInfoServiceImpl extends ServiceImpl<OrderPayInfoMapper, OrderPayInfo> implements IOrderPayInfoService {
    @Autowired
    private OrderPayInfoMapper orderPayInfoMapper;

    private static final Logger log = LoggerFactory.getLogger(OrderPayInfoServiceImpl.class);

    /**
     * 查询付款流水
     *
     * @param payUid 付款流水ID
     * @return 付款流水
     */
    @Override
    public OrderPayInfo selectOrderPayInfoById(Long payUid) {
        return this.getById(payUid);
    }

    @Override
    public OrderPayInfo getOrderPayInfo(String orderNo, String payMethod) {
        QueryWrapper<OrderPayInfo> queryWrapper = Wrappers.query();
        queryWrapper.eq("PAY_METHOD", payMethod);
        queryWrapper.eq("ORDER_NO", orderNo);
        List<OrderPayInfo> list = orderPayInfoMapper.selectList(queryWrapper);
        if (list.size() > 0)
            return list.get(0);
        return null;
    }

    /**
     * 查询付款流水列表
     *
     * @param orderPayInfo 付款流水
     * @return 付款流水
     */
    @Override
    public List<OrderPayInfo> queryList(OrderPayInfo orderPayInfo) {
        QueryWrapper<OrderPayInfo> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(orderPayInfo.getPayUid())) && orderPayInfo.getPayUid() != null) {
            queryWrapper.eq("PAY_UID", orderPayInfo.getPayUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayInfo.getCustomerUid())) && orderPayInfo.getCustomerUid() != null) {
            queryWrapper.eq("CUSTOMER_UID", orderPayInfo.getCustomerUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayInfo.getCustomerName())) && orderPayInfo.getCustomerName() != null) {
            queryWrapper.eq("CUSTOMER_NAME", orderPayInfo.getCustomerName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayInfo.getPayStatus())) && orderPayInfo.getPayStatus() != null) {
            queryWrapper.eq("PAY_STATUS", orderPayInfo.getPayStatus());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayInfo.getBeginTime())) && orderPayInfo.getBeginTime() != null) {
            queryWrapper.eq("BEGIN_TIME", orderPayInfo.getBeginTime());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayInfo.getEndTime())) && orderPayInfo.getEndTime() != null) {
            queryWrapper.eq("END_TIME", orderPayInfo.getEndTime());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayInfo.getPayTime())) && orderPayInfo.getPayTime() != null) {
            queryWrapper.eq("PAY_TIME", orderPayInfo.getPayTime());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayInfo.getPayMethod())) && orderPayInfo.getPayMethod() != null) {
            queryWrapper.eq("PAY_METHOD", orderPayInfo.getPayMethod());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayInfo.getOrderNo())) && orderPayInfo.getOrderNo() != null) {
            queryWrapper.eq("ORDER_NO", orderPayInfo.getOrderNo());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayInfo.getOrderType())) && orderPayInfo.getOrderType() != null) {
            queryWrapper.eq("ORDER_TYPE", orderPayInfo.getOrderType());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayInfo.getPayAmount())) && orderPayInfo.getPayAmount() != null) {
            queryWrapper.eq("PAY_AMOUNT", orderPayInfo.getPayAmount());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayInfo.getRealAmount())) && orderPayInfo.getRealAmount() != null) {
            queryWrapper.eq("REAL_AMOUNT", orderPayInfo.getRealAmount());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayInfo.getCurrencyUnit())) && orderPayInfo.getCurrencyUnit() != null) {
            queryWrapper.eq("CURRENCY_UNIT", orderPayInfo.getCurrencyUnit());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayInfo.getPayAccount())) && orderPayInfo.getPayAccount() != null) {
            queryWrapper.eq("PAY_ACCOUNT", orderPayInfo.getPayAccount());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayInfo.getPayRefSn())) && orderPayInfo.getPayRefSn() != null) {
            queryWrapper.eq("PAY_REF_SN", orderPayInfo.getPayRefSn());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayInfo.getIp())) && orderPayInfo.getIp() != null) {
            queryWrapper.eq("IP", orderPayInfo.getIp());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayInfo.getOpenid())) && orderPayInfo.getOpenid() != null) {
            queryWrapper.eq("OPENID", orderPayInfo.getOpenid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderPayInfo.getPlatSource())) && orderPayInfo.getPlatSource() != null) {
            queryWrapper.eq("PLAT_SOURCE", orderPayInfo.getPlatSource());
        }
        return orderPayInfoMapper.selectList(queryWrapper);
    }

    /**
     * 新增付款流水
     *
     * @param orderPayInfo 付款流水
     * @return 结果
     */
    @Override
    public boolean insertOrderPayInfo(OrderPayInfo orderPayInfo) {

        return this.save(orderPayInfo);

    }

    /**
     {
     "sp_appid":"wx8888888888888888",
     "sp_mchid":"1230000109",
     "sub_appid":"wxd678efh567hg6999",
     "sub_mchid":"1900000109",
     "out_trade_no":"1217752501201407033233368018",
     "trade_state_desc":"支付成功",
     "trade_type":"MICROPAY",
     "attach":"自定义数据",
     "transaction_id":"1217752501201407033233368018",
     "trade_state":"SUCCESS",
     "bank_type":"CMC",
     "success_time":"2018-06-08T10:34:56+08:00",
     "amount":{
     "payer_total":100,
     "total":100,
     "currency":"CNY",
     "payer_currency":"CNY"
     },
     "payer":{
     "openid":"oUpF8uMuAJO_M2pxb1Q9zNjWeS6o"
     },

     }
     */

    /**
     * 保存微信的支付回调记录
     *
     * @param jsonObject
     * @param parent
     * @return
     */
    @Override
    public boolean insertWechatOrderPayInfo(JSONObject jsonObject, OrderInfoParent parent) {
        OrderPayInfo op = new OrderPayInfo();
        op.setCreateTime(new Date());
        op.setUpdateTime(op.getCreateTime());
        op.setPayTime(op.getCreateTime());
        if (parent != null) {
            op.setCustomerUid(parent.getCustomerId());
            op.setPlatformId(parent.getPlatformId());
            op.setPayAmount(parent.getPayAmount());
        }
        String openId = jsonObject.getJSONObject("payer").getString("sub_openid");
        if (StringUtils.isEmpty(openId)){
            openId = jsonObject.getJSONObject("payer").getString("openid");
        }
        op.setCurrencyUnit(jsonObject.getString("currency"));
        op.setOpenid(openId);
        op.setOrderNo(jsonObject.getString("out_trade_no"));
        op.setRealAmount(jsonObject.getJSONObject("amount").getLong("payer_total"));
        op.setPayRefSn(jsonObject.getString("transaction_id"));
        op.setTransactionId(jsonObject.getString("transaction_id"));
        op.setSpAppid(jsonObject.getString("sp_appid"));
        op.setSubAppid(jsonObject.getString("sub_appid"));
        op.setSpMchid(jsonObject.getString("sp_mchid"));
        op.setSubMchid(jsonObject.getString("sub_mchid"));
        op.setPayMethod(PayMethodEnum.wechat.getKey());
        op.setPayStatus(PayStatusEnum.paid.getKey());
        op.setPayResult(jsonObject.toJSONString());
        op.setOrderType(parent.getOrderType());
        log.info("\n================保存支付记录:{}============\n",op);
        return save(op);
    }

    /**
     * 修改付款流水
     *
     * @param orderPayInfo 付款流水
     * @return 结果
     */
    @Override
    public boolean updateOrderPayInfo(OrderPayInfo orderPayInfo) {

        return this.updateById(orderPayInfo);
    }

    /**
     * 批量删除付款流水
     *
     * @param payUids 需要删除的付款流水ID
     * @return 结果
     */
    @Override
    public boolean deleteOrderPayInfoByIds(Long[] payUids) {
        return this.removeByIds(Arrays.asList(payUids));
    }


}
